<template>
  <div id="Cart">
    <div class="header">
      <p>100%正品保证</p>
      <p>所有商品精挑细选</p>
      <p>售后无忧</p>
    </div>
    <div class="cartnum">
      <p>购物数量</p>
    </div>
    <div class="cart-no" v-if="!shoplist.length">
      <div class="cartcontent">
        <div class="cartcontent-background"></div>
      </div>
      <div class="fs">
        <van-divider
          :style="{
            color: '#282828',
            borderColor: '#616161',
            padding: '0 16px',
          }"
          >热门推荐</van-divider
        >
      </div>
      <div class="recommend">
        <div
          class="recommend-box"
          v-for="(item, index) in cartlist"
          :key="index"
          @click="handlecart(item.id)"
        >
          <div class="imgbox">
            <img :src="item.image" alt="" />
          </div>
          <p>{{ item.store_name }}</p>
          <p class="p2">¥{{ item.price }}</p>
        </div>
      </div>
    </div>
    <div class="cart-yes">
      <van-checkbox
        checked-color="#ff448f"
        class="yes-bigbox"
        v-for="(itm, i) in shoplist"
        :key="i"
        v-model="itm.userLike"
        label-disabled
      >
        <div class="yes-imgbox">
          <img :src="itm.image" alt="" />
        </div>
        <div class="yes-text">
          <div class="title">
            {{ itm.store_name }}
          </div>
          <div class="text-box">
            <div class="price">{{ "¥" + itm.price }}</div>
            <div class="checknum">
              <div class="up" @click="handlenum(i, false)">-</div>
              <i>{{ itm.spec_type }}</i>
              <div class="down" @click="handlenum(i, true)">+</div>
            </div>
          </div>
        </div>
      </van-checkbox>

      <div class="footer" v-if="shoplist.length">
        <div class="footer-box">
          <van-checkbox
            v-model="allinfo.allcheck"
            checked-color="#ff448f"
            @click="handleallcheck"
            label-disabled
            >全选({{ allinfo.allNum }})</van-checkbox
          >
        </div>
        <div class="footer-box2">
          <p class="price">{{ "¥" + allinfo.allPrice + ".00" }}</p>
          <router-link class="git" :to="ischeck==true?'/':''">立即下单</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getcartlist } from "../api/Cart";
export default {
  data() {
    return {
      cartlist: [],
      isshow: false,
      idx: 0,
      num: 1,
      result: [],
      ischeck: false,
    };
  },
  created() {
    getcartlist().then((res) => {
      this.cartlist = res.data.data;
    });
  },
  computed: {
    shoplist() {
      return this.$store.state.cart.shoplist;
    },
    allinfo() {
      let { shoplist } = this;
      let allPrice = 0;
      let allNum = 0;
      let allcheck = true;
      for (let i = 0; i < shoplist.length; i++) {
        if (!shoplist[i].userLike) {
          allcheck = false;
        }
        if (shoplist[i].userLike) {
         allPrice += shoplist[i].price * this.shoplist[i].spec_type;
         allNum += this.shoplist[i].spec_type;
         this.ischeck=shoplist[i].userLike
        }
      }
      return { allPrice, allNum, allcheck };
    },
  },
  methods: {
    handlecart(id) {
      this.$router.push({ path: `/detail/${id}` });
    },
    handlenum(idx, bool) {
      this.$store.commit("cart/cartnummut", { idx, bool });
    },
    handleallcheck() {
      let { shoplist } = this;
      let bool = this.allinfo.allcheck;
      for (let i = 0; i < shoplist.length; i++) {
        this.shoplist[i].userLike = bool;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.van-checkbox__label {
  display: flex !important;
}
.checknum {
  width: 80px;
  display: flex;
  justify-content: space-between;
  .up,
  .down {
    width: 18px;
    line-height: 18px;
    background: #eee;
    border: 1px solid #000;
    text-align: center;
  }
}
#Cart {
  width: 100%;
  height: 90vh;
  padding-top: 0.5px;
  background-color: #f5f5f5;
  padding-bottom: 50px;
  .cart-no {
    padding-bottom: 50px;
  }
}
.header {
  width: 90%;
  height: 38px;
  padding: 0 5%;
  font-size: 11px;
  color: #8c8c8c;
  position: fixed;
  left: 0;
  background-color: #f5f5f5;
  z-index: 5;
  top: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.cartnum {
  width: 100%;
  height: 40px;
  background-color: #fff;
  padding: 0 15px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  font-size: 14px;
  color: #282828;
  position: fixed;
  left: 0;
  z-index: 5;
  top: 38px;
  display: flex;
  align-items: center;
}
.cartcontent {
  margin-top: 85px;
  background: #fff;
  padding-top: 0.5px;
}
.cartcontent-background {
  width: 207px;
  height: 168px;
  margin: 39px auto 28px auto;
  background-image: url(https://www.4g368.com/static/img/noCart.50b36491.png);
  background-position: 0% 0%;
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.fs {
  background: #fff;
  margin-top: -28px;
  padding: 16px;
}
.recommend {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  .recommend-box {
    margin-bottom: 15px;
    width: 167px;
  }
  .imgbox {
    width: 100%;
    height: 167px;
    border-radius: 10px;
    overflow: hidden;
    img {
      width: 100%;
      height: 167px;
    }
  }
  p {
    font-size: 14px;
    color: #282828;
    margin-top: 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 14px;
    color: #282828;
  }
  .p2 {
    font-size: 10px;
    color: #ff448f;
  }
}
.cart-yes {
  width: 100%;
  margin-top: 85px;
  margin-bottom: 98px;
  .yes-bigbox {
    padding: 12px 0.9vh;
    background-color: #fff;
    margin-bottom: 7px;
    display: flex;
    align-items: center;
    .yes-imgbox {
      width: 80px;
      height: 80px;
      overflow: hidden;
      float: left;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .yes-text {
      width: 31vh;
      float: right;
      margin-left: 10px;
      .title {
        font-size: 14px;
        color: #282828;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .attr {
        font-size: 12px;
        color: #868686;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .text-box {
        font-size: 16px;
        color: #282828;
        margin-top: 14px;
        display: flex;
        justify-content: space-between;
        .price {
          font-size: 16px;
          color: #282828;
        }
      }
    }
  }
  .footer {
    width: 91vw;
    height: 48px;
    background-color: #fafafa;
    position: fixed;
    bottom: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    .footer-box {
      display: flex;
      font-size: 14px;
      color: #282828;
      input {
        width: 24px;
        height: 20px;
        margin-right: 10px;
      }
    }
    .footer-box2 {
      width: 190px;
      height: 35px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .price {
        width: 80px;
        text-align: right;
        font-size: 15px;
        color: #ff448f;
      }
      .git {
        color: #fff;
        font-size: 15px;
        width: 94px;
        height: 35px;
        border: 0;
        border-radius: 25px;
        text-align: center;
        line-height: 35px;
        margin-left: 11px;
        background: #ff448f;
      }
    }
  }
}
</style>